<?php  
require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'PBNYFlickr.php';
  class PBNYHelper
  {
    static function createMenu()
    {
      JSubMenuHelper::addEntry("Seasons", "?option=com_pbny&c=season", JRequest::getWord('c', 'season') == 'season');
      JSubMenuHelper::addEntry("Divisions", "?option=com_pbny&c=division", JRequest::getWord('c', 'season') == 'division');
      JSubMenuHelper::addEntry("Conference", "?option=com_pbny&c=conference", JRequest::getWord('c', 'season') == 'conference');      
      JSubMenuHelper::addEntry("Teams", "?option=com_pbny&c=team", JRequest::getWord('c', 'season') == 'team');
      JSubMenuHelper::addEntry("Persons", "?option=com_pbny&c=person", JRequest::getWord('c', 'season') == 'person');
      JSubMenuHelper::addEntry("Players", "?option=com_pbny&c=player", JRequest::getWord('c', 'season') == 'player');
      JSubMenuHelper::addEntry("Schedules", "?option=com_pbny&c=schedule", JRequest::getWord('c', 'season') == 'schedule');
      #JSubMenuHelper::addEntry("Teams Game Stats", "?option=com_pbny&c=teamgamestat", JRequest::getWord('c', 'season') == 'teamgamestat');
      JSubMenuHelper::addEntry("Game Stats", "?option=com_pbny&c=gamestat", JRequest::getWord('c', 'season') == 'gamestat');
      JSubMenuHelper::addEntry("Players Game Stats", "?option=com_pbny&c=playergamestat", JRequest::getWord('c', 'season') == 'playergamestat');
	  JSubMenuHelper::addEntry("Bye", "?option=com_pbny&c=bye", JRequest::getWord('c', 'season') == 'bye');
      JSubMenuHelper::addEntry("Pics", "?option=com_pbny&c=pic", JRequest::getWord('c', 'season') == 'pic');
      JSubMenuHelper::addEntry("Videos", "?option=com_pbny&c=video", JRequest::getWord('c', 'season') == 'video');
	  JSubMenuHelper::addEntry("Positions", "?option=com_pbny&c=position", JRequest::getWord('c', 'season') == 'position');
      JSubMenuHelper::addEntry("Venues", "?option=com_pbny&c=venue", JRequest::getWord('c', 'season') == 'venue');
    }
    
    static function saveUpload($source, $name, $destination = null)
    {
      if(!$destination){
        $destination = JPATH_ROOT.DS.'images'.DS.'uploads';
        $result = '/images/uploads';
      }
      $destination .= DS.$name;
      $result.= "/$name";
      
      if(copy($source, $destination))
        return $result;
      else
        return null;
    }
    
    static function downloadImage($url)
    {  
      $destination = JPATH_ROOT.DS.'images'.DS.'uploads'.DS.basename($url);
      $result      = '/images/uploads/'.basename($url);
      #if(file_exists($destination))  //duplicate images issues
      #  return $result;
        
      $ch = curl_init($url);
      $fp = fopen($destination, 'wb');
      curl_setopt($ch, CURLOPT_FILE, $fp);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_exec($ch);
      curl_close($ch);
      fclose($fp);
      
      return $result;
    }
    
    static function sendUrlToFlickr($url)
    {
      $filepath = PBNYHelper::downloadImage($url);
      
      $f = new PBNYFlickr(JPATH_COMPONENT_ADMINISTRATOR.DS.'sample.ini');
      
      if(file_exists(JPATH_ROOT.DS.'images'.DS.'uploads'.DS.basename($filepath))){
        $result = $f->upload_photo(JPATH_ROOT.DS.'images'.DS.'uploads'.DS.basename($filepath));
        unlink($filepath);
        return $result;
      }
      else
        return null;
    }
    
    static function sendImageToFlickr($filepath)
    {
      $f = new PBNYFlickr(JPATH_COMPONENT_ADMINISTRATOR.DS.'sample.ini');
      
      if(file_exists(JPATH_ROOT.DS.'images'.DS.'uploads'.DS.basename($filepath))){
        $result = $f->upload_photo(JPATH_ROOT.DS.'images'.DS.'uploads'.DS.basename($filepath));
        unlink($filepath);
        return $result;
      }
      else
        return null;
    }
    
    static function getFlickrImageUrl($photo_id, $size = null)
    {
        if(empty($photo_id))
        return null;
      
      $f = new PBNYFlickr(JPATH_COMPONENT_ADMINISTRATOR.DS.'sample.ini');
      return $f->buildPhotoURL($photo_id, $size);
    }
    
  }
?>
